package factory;

import gamehandlers.GameHandler;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
import javax.imageio.ImageIO;
import sprite.Sprite;
import spritebehavior.Behavior;


/**
 * @author guytracy
 *         creates a collection of Sprites that contain a behavior
 *         and other parameters from an input text file.
 *         This factory can parse text files in the form:
 *         Behavior imagefile centerPoint(in form x,y) velocity(in form x,y)
 *         size(in form w,h) angle angularvelocity
 *         all of the above is in one line and is all of the information needed
 *         to create a sprite.
 *         Note: need to make behaviors have a sprite. use setSprite.
 */
public class SpriteFactory extends AbstractFactory {

    private Collection<Sprite> mySpriteList;
    private GameHandler myGameHandler;

    /**
     * makes a list of sprites from an input file
     * 
     * @param levelFile 
     * @param gh 
     * @return
     */
    public Collection<Sprite> getSprites (File levelFile, GameHandler gh) {
        mySpriteList = new ArrayList<Sprite>();
        myGameHandler = gh;
        loadObjects(levelFile);
        return mySpriteList;
    }

    @Override
    protected void addObject (Scanner line) {
        String spriteIDString = line.next();
        int spriteID = Integer.parseInt(spriteIDString);

        String className = BEHAVIOR_LOCATION + line.next();
        Class aClass = null;
        try {
            aClass = Class.forName(className);
        }
        catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            System.out.println("image not read");
        }
        Behavior newBehavior=null;
        try {
            newBehavior = (Behavior) aClass.newInstance();
        }
        catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        newBehavior.setGameHandler(myGameHandler);

        String spriteImageName = IMAGE_LOCATION + line.next();
        Image spriteImage = null;
        try {
            spriteImage = ImageIO.read(new File(spriteImageName));
        }
        catch (IOException e) {
            System.out.println("image not read");
        }

        String centerString = line.next();
        String[] splitCenter = centerString.split(",");
        Point centerPoint = new Point(Integer.parseInt(splitCenter[0]),
                Integer.parseInt(splitCenter[1]));

        String velocityString = line.next();
        String[] splitVelocity = velocityString.split(",");
        Point velocityPoint = new Point(Integer.parseInt(splitVelocity[0]),
                Integer.parseInt(splitVelocity[1]));

        String sizeString = line.next();
        String[] splitSize = sizeString.split(",");
        Dimension size = new Dimension(Integer.parseInt(splitSize[0]),
                Integer.parseInt(splitSize[1]));

        double angle = line.nextDouble();

        double angleVelocity = line.nextDouble();

        mySpriteList.add(new Sprite(spriteID, newBehavior, spriteImage,
                centerPoint, velocityPoint, size, angle, angleVelocity));
    }
}
